package com.ly.ocw.soap;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.springside.modules.mapper.JaxbMapper;

import com.ly.ocw.soap.memgra.MemgraEnvelope;
import com.ly.ocw.soap.memrec.MemrecEnvelope;
import com.ly.ocw.soap.telvalid.TelvalidEnvelope;
import com.ly.ocw.utils.HttpUtil;

public class CrmHttpUtils {
	
	//调用crm 接口 查询会员信息
	public static MemrecEnvelope memberRecordSearchReq(String url,String memid) throws ClientProtocolException, IOException{
		String body = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://haiziwang.com/hybris_crm/v1\">"
					  +" <soapenv:Header/>"
					  +"  <soapenv:Body>"
					  +"    <v1:MT_MemberRecordSearchReq>"
					  +"      <!--Zero or more repetitions:-->"
					  +"      <member>"
					  +"        <!--Optional:-->"
					  +"        <LOY_MEM_ID>"+ memid +"</LOY_MEM_ID>"
					  +"        <!--Optional:-->"
					  +"        <Source_ID>?</Source_ID>"
					  +"      </member>"
					  +"    </v1:MT_MemberRecordSearchReq>"
					  +"  </soapenv:Body>"
					  +"</soapenv:Envelope>";
//		String url = "http://localhost:8088/mockSI_TelephoneValidity_osBinding";
		
		String respStr = HttpUtil.httpPostForCrm(url, body);
		
		MemrecEnvelope resp = JaxbMapper.fromXml(respStr, MemrecEnvelope.class);
		return resp;
	}
	
	//会员等级查询
	public static MemgraEnvelope memberGradeSearchReq(String url,String memid) throws ClientProtocolException, IOException{
		String body = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://haiziwang.com/hybris_crm/v1\">"
				 +"<soapenv:Header/>"
				 +" <soapenv:Body>"
		         +" <v1:MT_MemberGradeSearchReq>"
		         +"    <!--Zero or more repetitions:-->"
		         +"    <row>"
		         +"        <!--Optional:-->"
		         +"       <LOY_MEM_ID>"+ memid +"</LOY_MEM_ID>"
		         +"    </row>"
		         +"  </v1:MT_MemberGradeSearchReq>"
		         +" </soapenv:Body>"
		         +"</soapenv:Envelope>";
//		String url = "http://localhost:8088/mockSI_TelephoneValidity_osBinding";
		
		String respStr = HttpUtil.httpPostForCrm(url, body);
		
		MemgraEnvelope resp = JaxbMapper.fromXml(respStr, MemgraEnvelope.class);
		return resp;
	}
	
	//
	public static TelvalidEnvelope telephoneValidityReq(String url,String mobile) throws ClientProtocolException, IOException{
		String body = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://haiziwang.com/hybris_crm/v1\">"+
				   "<soapenv:Header/>"+
				   "<soapenv:Body>"+
				     " <v1:MT_TelephoneValidityReq>"+
				       "  <row>"+
				         "   <TEL_NUMBER>"+ mobile +"</TEL_NUMBER>"+
				          "  <SMTP_ADDR></SMTP_ADDR>"+
				           " <NAME_NICK></NAME_NICK>"+
				           " <LOY_MEM_ID></LOY_MEM_ID>"+
				        " </row>"+
				     " </v1:MT_TelephoneValidityReq>"+
				  " </soapenv:Body>"+
				"</soapenv:Envelope>";
//		String url = "http://localhost:8088/mockSI_TelephoneValidity_osBinding";
		
		String respStr = HttpUtil.httpPostForCrm(url, body);
		TelvalidEnvelope resp = JaxbMapper.fromXml(respStr, TelvalidEnvelope.class);
		return resp;
	}
}
